package com.AdvancedDemos.Learn网络编程.CS架构.useTCP6图片上传;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 需求：图片服务器
 * <p>
 * 客户端：
 * 1.发送图片
 * 2.接收ok
 */
public class Client {

    public static void main(String[] args) throws IOException {

        System.out.println("【客户端】开启！");
        //客户端socket
        Socket socket = new Socket("127.0.0.1", 8888);
        System.out.println("【客户端】连接成功");

        //创建输入输出流
        FileInputStream is = new FileInputStream("F:\\360极速浏览器X下载\\波浪谷.jpg");
        OutputStream os = socket.getOutputStream();

        int len;
        byte[] bytes = new byte[1024 * 5];
        //先读
        while ((len = is.read(bytes)) != -1) {//从文件用字节流读取,直到文件结束
            os.write(bytes, 0, len);//立刻将读到的字节数组写出到socket管道中
        }

        //关闭文件流
        is.close();

        //告知服务器端发送完毕
        System.out.println("【客户端】通知服务器不再发送！");
        socket.shutdownOutput();
        //======================================================================

        System.out.println("【客户端】接收数据");
        //接收数据
        InputStream inputStream = socket.getInputStream();

        int len2;
        byte[] bytes2 = new byte[1024];

        while ((len2 = inputStream.read(bytes2)) != -1) {
            String s = new String(bytes2, 0, len2);
            System.out.println(s);
        }
        System.out.println("【客户端】关闭资源");
        //关闭资源
        socket.close();
    }
}
